package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public Department selectOne(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public List<Department> queryForData(QueryObject qo) {
        return departmentMapper.queryForData(qo);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Department> departments = departmentMapper.queryForData(qo);
        return new PageInfo<>(departments);

        /*int count = departmentMapper.queryForCount(qo);

        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),count,
                count == 0 ?
                Collections.emptyList() : departmentMapper.queryForData(qo));*/
    }

    /*@Override
    public int queryForCount(QueryObject qo) {
        int totalCount = departmentMapper.queryForCount(qo);
        return totalCount;
    }*/
}
